package com.sskj.bibi.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.bibi.http.HttpConfig;
import com.sskj.bibi.ui.fragment.PankouUpDownGradeFragment;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;


public class PankouUpDownGradeFragmentPresenter extends BasePresenter<PankouUpDownGradeFragment> {
    private WebSocketObserver stockSocketFive;
    private Disposable stockDisposableFive;
    private String code;

    public void getProduct(String qu,String code) {
        httpService.getProduct(qu,code)
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<CoinBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<CoinBean>>> response) {
                        HttpData<List<CoinBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            if (httpData.getData() != null && httpData.getData().size() > 0) {
                                Flowable.fromIterable(httpData.getData())
                                        .filter(stockCoinBean -> stockCoinBean.getCode().equals(code))
                                        .subscribe(productBean -> {
                                            mView.refreshCoin(httpData.getData().get(0));
                                        }, e -> {
                                        });
                            }
                        }
                    }
                });
    }

    public void getPankou(String code) {
        httpService.getPankou(code)
                .tag(this)
                .execute(new JsonCallBack<HttpData<List<WSFiveBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<WSFiveBean>>> response) {
                        HttpData<List<WSFiveBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            if (httpData.getData() != null && httpData.getData().size() > 0 && httpData.getData().get(0) != null) {
                                WSFiveBean wsFiveBean = httpData.getData().get(0);
                                mView.updateFive(wsFiveBean);
                            } else {
                                WSFiveBean wsFiveBean = new WSFiveBean();
                                wsFiveBean.setCode(code);
                                wsFiveBean.setAsks(new ArrayList<>());
                                wsFiveBean.setBids(new ArrayList<>());

                                mView.updateFive(wsFiveBean);
                            }
                        }
                    }
                });
    }

//    public void getRate(String code) {
//        httpService.getCoinFee(code)
//                .execute(new JsonCallBack<HttpData<CoinFee>>() {
//                    @Override
//                    public void onSuccess(Response<HttpData<CoinFee>> response) {
//                        HttpData<CoinFee> httpData = response.body();
//                        if (httpData.getStatus() == HttpConfig.OK) {
//                            mView.updateFee(httpData.getData());
//                        }
//                    }
//                });
//    }

    public void initSocket(String code) {
        if (code==null)
            return;
        code= CommonUtil.dealReuqestCode(code);
        if (stockSocketFive == null) {
            stockSocketFive = httpService.pushCoinFive(code);
            stockDisposableFive = stockSocketFive
                    .map(s -> new Gson().fromJson(s, WSFiveBean.class))
                    .subscribe(data -> RxBus.getDefault().post(data), e -> {
                        System.out.println(e);
                    });
        } else {
            stockSocketFive.send("{\"unsub\":\"depth@"+this.code+"\"}");
            stockSocketFive.send("{\"sub\":\"depth@"+code+"\"}");
        }
        this.code=code;
    }

    public void sendCode(String code) {

        initSocket(code);
    }

    public void closeWebSocket() {
        stockSocketFive = null;

        DisposUtil.close(stockDisposableFive);
    }
}
